package chain_apply

import (
	"chain_bridge_service/configs"
	"chain_bridge_service/internal/pkg/core"
	"chain_bridge_service/internal/repository/mongodb/chain_apply"
	"chain_bridge_service/internal/repository/mongodb/user"
	"context"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"time"
)

type ResponseData struct {
	ChainName0 string `json:"chainName0"`
	ChainName1 string `json:"chainName1"`
	Status     string `json:"status"`
	TokenName0 string `json:"tokenName0"`
	TokenName1 string `json:"tokenName1"`
	TimeStamp  string `json:"timeStamp"`
	Amount0    string `json:"amount0"`
	Amount1    string `json:"amount1"`
}

type ResponseAdminData struct {
	TxHash    string `json:"txHash"`
	FromChain string `json:"fromChain"`
	ToChain   string `json:"toChain"`
	Address   string `json:"address"`
	Amount    string `json:"amount"`
	Status    string `json:"status"`
	Timestamp int64  `json:"timestamp"`
}

func (s *service) List(ctx core.Context, userId string, from string, pageIndex uint, pageSize uint) (chainApplyTotal int64, chainApplyList []ResponseData, err error) {
	model := chain_apply.NewModel()
	model.UserId = userId
	model.From = from
	total, err := model.Count(s.db.GetDb())
	list, err := model.List(s.db.GetDb(), pageIndex, pageSize)
	cfg := configs.Get().Mongodb
	chainConfigClient := s.db.GetDb().Database(cfg.Config.Name).Collection("chain_config")
	chainTokenClient := s.db.GetDb().Database(cfg.Config.Name).Collection("chain_token")

	var responseList []ResponseData

	var chainConfig struct {
		ChainName string `bson:"chainName"`
	}
	var chainToken struct {
		Symbol string `bson:"symbol"`
	}

	loc, _ := time.LoadLocation("Asia/Shanghai")
	for _, apply := range list {
		var data ResponseData
		configId0, _ := primitive.ObjectIDFromHex(apply.ChainConfigId0)
		configId1, _ := primitive.ObjectIDFromHex(apply.ChainConfigId1)
		tokenId0, _ := primitive.ObjectIDFromHex(apply.TokenId0)
		tokenId1, _ := primitive.ObjectIDFromHex(apply.TokenId1)
		_ = chainConfigClient.FindOne(context.TODO(), bson.D{{"_id", configId0}}).Decode(&chainConfig)
		data.ChainName0 = chainConfig.ChainName
		_ = chainConfigClient.FindOne(context.TODO(), bson.D{{"_id", configId1}}).Decode(&chainConfig)
		data.ChainName1 = chainConfig.ChainName
		_ = chainTokenClient.FindOne(context.TODO(), bson.D{{"_id", tokenId0}}).Decode(&chainToken)
		data.TokenName0 = chainToken.Symbol
		_ = chainTokenClient.FindOne(context.TODO(), bson.D{{"_id", tokenId1}}).Decode(&chainToken)
		data.TokenName1 = chainToken.Symbol
		data.Status = apply.Status
		data.Amount0 = apply.Amount0
		data.Amount1 = apply.Amount1
		data.TimeStamp = time.Unix(apply.TimeStamp, 0).In(loc).Format("2006-01-02 15:04:05")
		responseList = append(responseList, data)
	}
	return total, responseList, err
}

func (s *service) GetAdminList(ctx core.Context, pageIndex uint, pageSize uint) (chainApplyTotal int64, chainApplyList []ResponseAdminData, err error) {
	userInfo := ctx.GetInfo("userInfo").(*user.User)
	model := chain_apply.NewModel()
	model.UserId = userInfo.Id
	model.TxHash = ctx.RequestInputParams().Get("txHash")
	model.From = ctx.RequestInputParams().Get("address")
	total, err := model.Count(s.db.GetDb())
	list, err := model.List(s.db.GetDb(), pageIndex, pageSize)
	cfg := configs.Get().Mongodb
	chainConfigClient := s.db.GetDb().Database(cfg.Config.Name).Collection("chain_config")

	var responseList []ResponseAdminData
	var chainConfig struct {
		ChainName string `bson:"chainName"`
	}

	for _, apply := range list {
		var data ResponseAdminData
		configId0, _ := primitive.ObjectIDFromHex(apply.ChainConfigId0)
		configId1, _ := primitive.ObjectIDFromHex(apply.ChainConfigId1)

		_ = chainConfigClient.FindOne(context.TODO(), bson.D{{"_id", configId0}}).Decode(&chainConfig)
		data.FromChain = chainConfig.ChainName
		_ = chainConfigClient.FindOne(context.TODO(), bson.D{{"_id", configId1}}).Decode(&chainConfig)
		data.ToChain = chainConfig.ChainName

		data.Status = apply.Status
		data.TxHash = apply.TxHash
		data.Address = apply.From
		data.Timestamp = apply.TimeStamp
		data.Amount = apply.Amount0
		responseList = append(responseList, data)
	}
	return total, responseList, err
}
